package com.java.util;

import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CommonUtil {
	
	public static final String NULL = "-";
	
	public static final String PERSONAL = "个人";
	
	public static final String BUSINESS_COMMON = "普通企业";
	
	public static final String BUSINESS_GUARANTEED = "担保企业";
	
	public static final String enable = "启用";
	
	public static final String unable = "锁定";
	
	public static final String WAIT = "待开户";
	
	public static final String WAIT_INPUT = "待补录";
	
	public static final String AUDITING = "审核中";
	
	public static final String OPEN = "已开户";

	public static final String stash = "草稿";
	
	public static final String submit_inner = "内部审核";
	
	public static final String audit_pass = "内部审核通过";
	
	public static final String audit_reject = "内部审核驳回";
	
	public static final String submit_third = "提交第三方审核";
	
	public static final String audit_third_pass = "第三方审核通过";
	
	public static final String audit_third_reject = "第三方审核驳回";
	
	public static final String audit_thrid_wait_makeup = "第三方待补录";
	
	public static final String audit_thrid_process = "第三方待审中";
	
	public static final String audit_third_wait_img = "第三方待审材料中";
	
	public static final String audit_third_error = "第三方审核异常";
	
	public static final String rescinded = "已撤销";
	
	public static final String actived = "激活";
	
	public static final String unActived = "取消激活";
	
	public static final String WORKDAYANDT0 = "当天计息";
	
	public static final String WORKDAYANDT1 = "T+1工作日起息";
	
	public static final String NATURALDAYANDT1 = "D+1起息";
	
	public static final String WAITANNOUNCED = "待发布";
	
	public static final String FUNDRAISING = "筹款中";
	
	public static final String ALREADOFFLINE = "已下线";
	
	public static final String WAITREPAYMENTS = "待还款";
	
	public static final String ALREADLOANING = "已垫资";
	
	public static final String ALREADYOVERDUE = "已逾期";
	
	public static final String ALREADYREPAYMENT = "已还款";
	
	public static final String ALREADYABANDONED = "已废标";
	
	public static final String FULLBID = "满标";
	
	public static final String SPLIT = "=SPLIT=";
	
	public static final String REGEX_NUM = "^\\+?[1-9][0-9]*$";
	
	public static String getCardNumber(String cardNumber){
		if (cardNumber.length() == 15) {
			return cardNumber.substring(0, 8) + "****" + cardNumber.substring(12, 15);
		}
		
		return cardNumber.substring(0, 10) + "****" + cardNumber.substring(14, 18);
	}
	
	public static String maskIdCardNo(String cardNumber){
		if (cardNumber.length() == 18) {
			return cardNumber.substring(0,10) + "********";
		}
		
		return cardNumber;
	}
	
	public static String maskPhone(String cardNumber){
		if (cardNumber.length() == 11) {
			return cardNumber.substring(0,4) + "***" + cardNumber.substring(7,11);
		}
		
		return cardNumber;
	}
	
	/**
	 * obj 2 json字符串回传前台 
	 * 保留vo的null属性
	 * 替换空串和null 为 "-"
	 * @param obj
	 * @return 
	 * @throws JsonProcessingException
	 */
	public static String toJsonStr(Object obj) throws JsonProcessingException{
		ObjectMapper mapper = new ObjectMapper();
		
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));//date类型转换格式
		mapper.setSerializationInclusion(Include.ALWAYS);//设置为always 序列化时 保留null属性
		
		return mapper.writeValueAsString(obj)
				.replace(":\"\"", ":\"-\"")//将空串转换为"-"
				.replace(":true", ":\"是\"")
				.replace(":false", ":\"否\"")
				.replace(":null", ":\"-\"");//将null转换为"-"
	}
	
	public static boolean regExMatch(String patternStr, String checkStr){
		Pattern pattern = Pattern.compile(patternStr);
		if (!StringUtils.isEmpty(checkStr)) {
			return pattern.matcher(checkStr).matches();
		}
		return false;
	}
	
	public static String formatPeriod(String periodStr){
		StringBuffer sb = new StringBuffer(periodStr);
		if(periodStr.startsWith("-")){
			sb.insert(0, "#");
		}
		if(periodStr.endsWith("-")){
			sb.append("#");
		}
		return sb.toString();
	}
	
	public static void setResponseParams(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET, POST");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
	}
	
	public static void main(String[] args) {
		//System.out.println(getCardNumber("130503670401001"));
		System.out.println(formatPeriod("2016-09-18 11:03:33"));
		
		System.out.println(CommonUtil.regExMatch(CommonUtil.REGEX_NUM, "a111"));
		System.out.println(CommonUtil.maskIdCardNo("42122319930219007X"));
		System.out.println(CommonUtil.maskPhone("18500700410"));
	}
	
}
